/**
 * 
 * 深圳市店联电子商务有限公司
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TCommodity;
import com.ruipengkj.bean.TGift;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelComboBoxComponent2;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.comboBox.CommodityJComboBoxTableComponent;
import com.ruipengkj.service.TGiftService;
import com.ruipengkj.view.base.BaseDialog;
import com.ruipengkj.view.systemView.SelectUserPanel;

/**
 * 说明：
 * 增加满就送Dialog
 * @Author：朱攀
 * @Date：2012-07-12
 */
public class GiftAddDialog extends BaseDialog {


	
	private static final long serialVersionUID = 1L;
	
	private JButton okButton;//确定
	private JButton cButton;//取消
	private JLabelJTextFieldComponent giftID=null;//满就送编号
	private JLabelJTextFieldComponent valve=null;//阀值
	private JLabelJTextFieldComponent money=null;//赠送的金额
	//private JLabelJComboBoxComponent commodityID=null;//手机号
	private JLabelJComboBoxComponent  gType;   //满就送类型 1 为赠送金额 ，2为送商品
	
	private JLabelComboBoxComponent2 giftCommodity; //选择商品
	
	
	
	
	
	public boolean isSuccess=false;  //判断是否成功
	
	
	private  TGiftService  tGiftService;
	public GiftAddDialog(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		tGiftService = BuildSpringApplication.getInstance().getTGiftServiceImpl();
		this.init();
		this.setSize(400, 500);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		
		this.initListener();
	}
	
	private void initViews(){
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		
		
		giftID=new JLabelJTextFieldComponent("编号：",30);
		giftID.getText().setText(UUID.GenUUID15());  //设置默认编号
		giftID.getText().setEditable(false);   //把文本框设为只读
		valve = new JLabelJTextFieldComponent("阀值：", 30);
		money=new JLabelJTextFieldComponent("赠送金额：",30);
		//commodityID=new JLabelJTextFieldComponent("商品编号：",30);
		gType = new JLabelJComboBoxComponent("赠送类型：",ApplicationContext.GIFT_MAP(),187);
		
		giftCommodity = new JLabelComboBoxComponent2("选择商品：", new CommodityJComboBoxTableComponent(), 188);
		
		JPanel jp1=new JPanel();
		okButton=new JButton("保存");
		cButton=new JButton("关闭");
		jp1.add(okButton);
		jp1.add(cButton);
		main_jp.add(giftID);
		main_jp.add(valve);
		main_jp.add(money);
		main_jp.add(gType);
		main_jp.add(giftCommodity);
		
		jp1.setOpaque(false);
		main_jp.add(jp1);
		this.add(main_jp);
	}
	private void initListener(){
		okButton.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		cButton.addActionListener(this);
		cButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	
	
	
	
	

	
	
	public void showDialog(TGift gift){
		if(gift!=null){
			
			
			giftID.getText().setText(gift.getGiftID());
			valve.getText().setText(gift.getValve()+"");
			money.getText().setText(gift.getMoney()+"");
			gType.getBox().setSelectedItem(ApplicationContext.GIFT_MAP().get(gift.getType()));
			TCommodity c = (TCommodity)giftCommodity.getBox().getSelectedItem();
			if(c != null)
			{
				giftCommodity.getBox().setName(c.getName())	;
			}
			
			
		}
		this.setVisible(true);
	}
	
	
	public void actionPerformed(ActionEvent e) {
		
		
		if(e.getSource()==okButton){
			//提交
			
			String gid=giftID.getText().getText().trim();//编号
			String gValve=valve.getText().getText().trim(); //阀值
			String gmoney=money.getText().getText().trim();//赠送金额
			String gcommodityID=""; //赠送的商品编号
			
			Byte gtype=(Byte)gType.getBox().getSelectedItem();
			TCommodity c = (TCommodity)giftCommodity.getBox().getSelectedItem();
			if(c != null)
			{
				gcommodityID=	c.getCommodityID();
			}
			
			
			
			
			if(!MatcherFormatUtil.MatcherNumberScheme(gValve)||gValve.length()>8){
				JOptionPane.showMessageDialog(null, "阀值只能为数字，且不能大于8位");
				valve.getText().requestFocus();
				return;
			}
			
			if(!MatcherFormatUtil.MatcherPriceScheme(gmoney)){
				JOptionPane.showMessageDialog(null, "价格格式不正确，且小数点左边不能大于10位，右边不能大于2位");
				money.getText().requestFocus();
				return;
			}
			
			
			Date date=new Date();
			//TSendCardRecord tSendCardRecord=new TSendCardRecord();
			//tSendCardRecord.setSendCardRecordID(UUID.GenUUID15());
			//tSendCardRecord.setCardNum(cardNum);
			//tSendCardRecord.setSendCardTime(date);
			TGift tGift =new  TGift();
			tGift.setCommodityID(gcommodityID);
			tGift.setGiftID(gid);
			tGift.setMoney(Double.parseDouble(gmoney));
			tGift.setValve(Double.parseDouble(gValve));
			tGift.setType(gtype);
			
			
			//tUser.getTSendCardRecords().add(tSendCardRecord);
			try {
				tGiftService.saveOrUpdateService(tGift);  // 掉用增加方法
				BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("修改会员，卡号为：+"+gid);
				JOptionPane.showMessageDialog(null, "操作成功");
				SelectUserPanel s=new SelectUserPanel();
				isSuccess=true;
				this.dispose();
				 
			} catch (Exception e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "操作失败，请重新操作");
				isSuccess=false;
				
			}
		}else if(e.getSource() == cButton) {
			if(Util.showConfirmDialog("是否要取消？")==0){
				isSuccess=false;
				this.dispose();
			}
		}
	}
	

	
//	public static void main(String[] args)
//	{
//		JFrame f = new JFrame();
//		f.setSize(300,300);
//		f.setTitle("选择商品");
//		JComboBox c = new CommodityJComboBoxTableComponent();
//		f.getContentPane().add(c);
//		f.setVisible(true);
//	}
}
